package com.wlyuan.index.domain.source.assembler;

import com.wlyuan.core.marker.NamedUtils;
import com.wlyuan.core.marker.ValueUtils;
import com.wlyuan.finance.dto.waybillfinance.WaybillFinanceRelationEventDTO;
import com.wlyuan.index.api.dto.waybill.BalanceIndexDTO;
import com.wlyuan.index.domain.source.utils.SourceUtils;
import com.wlyuan.index.domain.source.valueobj.Balance;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.var;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class BalanceAssembler {
    @Mapper(uses = {ValueUtils.class, NamedUtils.class, SourceUtils.class})
    public interface Assembler {
        @Mapping(target = "tenantId", source = "relation.tenantId")
        @Mapping(target = "status", source = "relation.financeStatus")
        @Mapping(target = "arrivePayFee", source = "source.arrivePayFeeFlag")
        @Mapping(target = "collectionFee", source = "source.collectionFeeFlag")
        @Mapping(target = "prepayFee", source = "source.prepayFeeFlag")
        @Mapping(target = "receiptFee", source = "source.receiptFeeFlag")
        @Mapping(target = "paymentFee", source = "source.waybillFeeFlag")
        Balance assemble(BalanceIndexDTO source, WaybillFinanceRelationEventDTO relation);
    }

    private static final Assembler ASSEMBLER = Mappers.getMapper(Assembler.class);

    public static List<Balance> assemble(BalanceIndexDTO source) {
        if (CollectionUtils.isEmpty(source.getRelations())) {
            return Collections.emptyList();
        }
        var balances = new ArrayList<Balance>();
        source.getRelations().forEach(relation -> balances.add(ASSEMBLER.assemble(source, relation)));
        return balances;
    }
}
